import React from 'react';
import { observer, inject } from 'mobx-react';
import { getAntd,getEcCom,getLabel } from '../../util/convert';
import * as _ from '../../util/public/common';

@observer
export default class BatchPrint extends React.Component{
    constructor(){
        super();
    }
    getUrlStr(){
        const { location } = this.props;
        const urlParams = location.query || {};
        const range = ["ismonitor"];
        let urlStr = "";
        for(const key in urlParams){
            if(_.includes(range, key))
                urlStr += `&${key}=${urlParams[key]}`;
        }
        return urlStr;
    }
    render(){
        const { Button } = getAntd();
        const { WeaRightMenu } = getEcCom();
        const location = this.props.location || {};
        const printstr = location.query && location.query.printstr;
        const printArr = printstr.split(",");
        const urlStr = this.getUrlStr();
        
        let printrequestids = "";
        printArr.length > 0 && printArr.map(item =>{
            const itemArr = item.split("|");
            const requestid = itemArr[0];
            if(itemArr.length === 2){
                printrequestids+=","+requestid;
            }
        })
        if(printrequestids !== "")
            printrequestids = printrequestids.substring(1);
        window.batchPrintRequestidNum = printArr.length;
        window.batchPrintRequestids = printrequestids;

        return (
            <div className="wf-batch-print">
                {
                    printArr.length > 0 && printArr.map((item,index) =>{
                        const itemArr = item.split("|");
                        const requestid = itemArr[0];
                        const modeid = itemArr[1];
                        if(itemArr.length === 2){
                            const src = `/workflow/request/PrintRequestSPA.jsp?isBatchPrint=true&ismode=2&modeid=${modeid}&requestid=${requestid}${urlStr}`;
                             return <div className="wf-batch-print-item" style={{textAlign:"center", pageBreakBefore:index==0 ? "avoid" : "always"}}>
                                <iframe id={"request_"+requestid} src={src} style={{border:0,width:'100%',height:'100%'}}></iframe>
                            </div>
                        }
                    })
                }
            </div>
        )
    }
    getRightMenu(){
        let btns = [];
        btns.push({
            key:"print",
    		icon: <i className='icon-coms-Print'/>,
            content:getLabel(-1,'打印'),
            onClick: key =>{
                window.print();
            }
    	});
        return btns;
    }
}